﻿using System;
using System.IO.Ports;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Linq;
using AEM.Data;
using AEM.Data.Enum;
using AEM.Utils;
using Actions = AEM.Data.BLL.Actions;

namespace AEM.Basic
{
    /// <summary>
    /// LiftInfo.xaml 的交互逻辑
    /// </summary>
    public partial class LiftInfo : Window
    {
        public int index = 0;
        public LiftInfo()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LiftInfo_Loaded);
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(LiftInfo_MouseLeftButtonDown);
            this.liftinfoList.SelectionChanged += new SelectionChangedEventHandler(liftinfoList_SelectionChanged);
            this.liftinfoList.MouseLeftButtonDown += new MouseButtonEventHandler(liftinfoList_MouseLeftButtonDown);

            this.liftGroup.SelectionChanged += new SelectionChangedEventHandler(liftGroup_SelectionChanged);

            this.liftAdd.Click += new RoutedEventHandler(liftAdd_Click);
            this.liftEdit.Click += new RoutedEventHandler(liftEdit_Click);
            this.liftDelete.Click += new RoutedEventHandler(liftDelete_Click);

            this.KeyUp += new KeyEventHandler(LiftInfo_KeyUp);
            this.liftType.SelectionChanged += new SelectionChangedEventHandler(liftType_SelectionChanged);

            this.addmore.Click += new RoutedEventHandler(addmore_Click);
            this.okForMore.Click += new RoutedEventHandler(okForMore_Click);
            this.delForMore.Click += new RoutedEventHandler(delForMore_Click);
            this.add343.Click += new RoutedEventHandler(add343_Click);
        }

        void liftGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int groupindex = Convert.ToInt32(liftGroup.SelectedItem);
            int count = 0;
            if (null != Config.LiftInftList)
                count = (from Data.Model.LiftInfo l in Config.LiftInftList
                         where l.LiftGroup == groupindex && l.LiftType == Data.Enum.LiftType.电梯
                         select l).Count();
            if (count >= 20)
            {
                errmsg.Text = this.TryFindResource("GroupRestriction") as string;

                int index = liftGroup.SelectedIndex;
                if (index == liftGroup.Items.Count - 1) index = -1;
                liftGroup.SelectedIndex = index + 1;

            }

        }

        void liftType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (2 != this.liftType.SelectedIndex)
            {
                anglelabel.Visibility = Visibility.Hidden;
                liftAngle.Visibility = Visibility.Hidden;
            }
            else
            {
                anglelabel.Visibility = Visibility.Visible;
                liftAngle.Visibility = Visibility.Visible;
            }
            if (0 != this.liftType.SelectedIndex)
            {
                floorLabel.Visibility = Visibility.Hidden;
                floorExample.Visibility = Visibility.Hidden;
                liftActualFloor.Visibility = Visibility.Hidden;
            }
            else
            {
                floorLabel.Visibility = Visibility.Visible;
                floorExample.Visibility = Visibility.Visible;
                liftActualFloor.Visibility = Visibility.Visible;
            }
        }

        void LiftInfo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        void LiftInfo_Loaded(object sender, RoutedEventArgs e)
        {

            this.liftinfoList.ItemsSource = Actions.LiftInfo.List("");
            this.liftinfoList.Items.Refresh();
            //liftType.ItemsSource = StringHelper.EnumToList<LiftType>();//这一句是用来绑定COMBOBOX和子选项
            //liftType.SelectedIndex = 0;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            liftCOM.ItemsSource = ports;
            liftCOM.SelectedIndex = 0;
            liftGroup.ItemsSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            liftGroup.SelectedIndex = 0;
            liftLine.ItemsSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            liftLine.SelectedIndex = 0;
            liftLineSerial.ItemsSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
            liftLineSerial.SelectedIndex = 0;
            liftScreen.ItemsSource = new int[] { 1, 2, 3, 4, 5 };
            liftScreen.SelectedIndex = 0;
            typeLibNum.ItemsSource = Data.DAL.TypeLib.List("");
            typeLibNum.DisplayMemberPath = "TypeLibNum";
            typeLibNum.SelectedValuePath = "TypeLibNum";
            typeLibNum.SelectedIndex = 0;
            liftType.SelectedIndex = 0;

            this.liftNum.TextChanged += new TextChangedEventHandler(liftNum_TextChanged);
        }

        void liftinfoList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            liftinfoList_SelectionChanged(sender, null);
        }

        void liftinfoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Data.Model.LiftInfo row = (sender as ListView).SelectedItem as Data.Model.LiftInfo;
            if (null != row)
            {
                liftNum.Text = row.LiftNum.ToString(); ;
                liftType.SelectedValue = (int)row.LiftType;//可以参照这里
                liftGroup.SelectedItem = row.LiftGroup;
                liftLine.SelectedItem = row.LiftLine;
                liftLineSerial.SelectedItem = row.LiftLineSerial;
                liftActualFloor.Text = row.LiftActualFloor;
                liftModel.Text = row.LiftModel;
                liftSite.Text = row.LiftSite;
                liftCOM.SelectedValue = row.LiftCOM;
                liftScreen.SelectedItem = row.Screen;
                typeLibNum.SelectedValue = row.TypeLibNum;
                liftShortName.Text = null != row.ShortName ? row.ShortName : row.LiftNum + "#";
            }
        }

        void liftNum_TextChanged(object sender, TextChangedEventArgs e)
        {
            errmsg.Text = "";
            if (!string.IsNullOrEmpty(this.liftNum.Text.Trim()) && !this.liftNum.Text.Trim().isNumeric())
                this.liftNum.Text = this.liftNum.Text.Substring(0, liftNum.Text.Length - 1);
            if (this.liftNum.Text.toInt32() > 255)
            {
                this.liftNum.Text = "255";
                errmsg.Text = TryFindResource("FormatLiftNum") as string;
            }
            if (liftNum.Text.toInt32() < 1)
            {
                this.liftNum.Text = "1";
                errmsg.Text = TryFindResource("FormatLiftNum") as string;
            }
        }

        void liftAdd_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (string.IsNullOrEmpty(liftNum.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftNum") as string); return; }
            if (string.IsNullOrEmpty(liftSite.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftSite") as string); return; }
            //if (string.IsNullOrEmpty(liftModel.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftModel") as string); return; }
            if (string.IsNullOrEmpty(liftShortName.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("ShortName") as string); return; }
            if (0 == this.liftType.SelectedIndex)
            {
                if (string.IsNullOrEmpty(liftActualFloor.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftActualFloor") as string); return; }
                if (!this.liftActualFloor.Text.checkActualFloor()) { errmsg.Text = this.TryFindResource("FormatLiftActualFloor") as string; return; }
            }
            
            Data.Model.LiftInfo row = new Data.Model.LiftInfo();
            row = GetLiftFromUI(row);

            Actions.LiftInfo.AddLiftResult ret = Actions.LiftInfo.AddLiftInfo(row);
            switch (ret)
            {
                case Actions.LiftInfo.AddLiftResult.LiftInfoIsNull:
                    errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftInfoAdd") as string);
                    break;
                case Actions.LiftInfo.AddLiftResult.LiftNumAlreadyExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("LiftNum") as string);
                    break;
                case Actions.LiftInfo.AddLiftResult.OK:
                    errmsg.Text = this.TryFindResource("successfuladd") as string;
                    this.liftinfoList.ItemsSource = Config.LiftInftList = Actions.LiftInfo.List("");
                    this.liftinfoList.Items.Refresh();
                    string msg = "=InfoAddLift," + liftNum.Text.Trim();
                    Actions.Log.AddOperation.Invoke(msg);//string.Format((string)this.TryFindResource("InfoAddLift"), liftNum.Text.Trim())
                    liftNum.Text = "1";
                    liftType.SelectedIndex = 0;
                    liftGroup.SelectedIndex = 0;
                    liftLine.SelectedIndex = 0;
                    liftLineSerial.SelectedItem = 0;
                    liftCOM.SelectedIndex = 0;
                    liftScreen.SelectedIndex = 0;
                    typeLibNum.SelectedIndex = 0;
                    liftActualFloor.Text = "";
                    liftModel.Text = "";
                    liftSite.Text = "";
                    liftShortName.Text = "";
                    liftAngle.SelectedItem = 0;
                    if (null != Actions.LiftInfo.ReLoadUI)
                        Actions.LiftInfo.ReLoadUI();
                    break;
            }
        }

        void liftEdit_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (null == this.liftinfoList.SelectedItem) { errmsg.Text = this.TryFindResource("SelectOne") as string; return; }
            if (string.IsNullOrEmpty(liftNum.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftNum") as string); return; }
            if (string.IsNullOrEmpty(liftSite.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftSite") as string); return; }
            //if (string.IsNullOrEmpty(liftModel.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftModel") as string); return; }
            if (string.IsNullOrEmpty(liftShortName.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("ShortName") as string); return; }
            if (0 == this.liftType.SelectedIndex)
            {
                if (string.IsNullOrEmpty(liftActualFloor.Text.Trim())) { errmsg.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("LiftActualFloor") as string); return; }
                if (!this.liftActualFloor.Text.checkActualFloor()) { errmsg.Text = this.TryFindResource("FormatLiftActualFloor") as string; return; }
            }

            Data.Model.LiftInfo item = this.liftinfoList.SelectedItem as Data.Model.LiftInfo;

            Data.Model.LiftInfo row = new Data.Model.LiftInfo();
            row.ID = item.ID;
            row = GetLiftFromUI(row);

            Actions.LiftInfo.ModifiyliftResult ret = Actions.LiftInfo.ModifiyLiftInfo(row);
            switch (ret)
            {
                case Data.BLL.Actions.LiftInfo.ModifiyliftResult.LiftInfoIsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("LiftInfoAdd") as string);
                    break;
                case Data.BLL.Actions.LiftInfo.ModifiyliftResult.LiftIsNotExist:
                    errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("LiftInfoAdd") as string);
                    break;
                case Data.BLL.Actions.LiftInfo.ModifiyliftResult.LiftNumAlreadyExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("LiftNum") as string);
                    break;
                case Data.BLL.Actions.LiftInfo.ModifiyliftResult.OK:
                    errmsg.Text = this.TryFindResource("successfulupdate") as string;
                    this.liftinfoList.ItemsSource = Config.LiftInftList = Actions.LiftInfo.List("");
                    this.liftinfoList.Items.Refresh();
                    string msg = "=InfoEditLift," + liftNum.Text.Trim();
                    Actions.Log.AddOperation.Invoke(msg);//string.Format((string)this.TryFindResource("InfoEditLift"), liftNum.Text.Trim()));
                    liftNum.Text = "1";
                    liftType.SelectedIndex = 0;
                    liftGroup.SelectedIndex = 0;
                    liftLine.SelectedIndex = 0;
                    liftLineSerial.SelectedIndex = 0;
                    liftActualFloor.Text = "";
                    liftModel.Text = "";
                    liftSite.Text = "";
                    liftCOM.SelectedIndex = 0;
                    typeLibNum.SelectedIndex = 0;
                    liftShortName.Text = "";
                    liftAngle.SelectedItem = 0;
                    liftScreen.SelectedIndex = 0;
                    if (null != Actions.LiftInfo.ReLoadUI)
                        Actions.LiftInfo.ReLoadUI();
                    break;
            }
        }

        void liftDelete_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            Data.Model.LiftInfo row = this.liftinfoList.SelectedItem as Data.Model.LiftInfo;
            if (null == row) { errmsg.Text = this.TryFindResource("SelectOne") as string; return; }
            if (MessageBoxResult.Yes == MessageBox.Show(string.Format(this.TryFindResource("AskForDelete") as string, row.LiftNum + "(" + row.LiftSite + ")"), this.TryFindResource("MessageBoxTitle") as string, MessageBoxButton.YesNo, MessageBoxImage.Warning))
            {
                Actions.LiftInfo.DeleteliftResult ret = Actions.LiftInfo.DeleteLiftInfo(row.ID);
                switch (ret)
                {
                    case Data.BLL.Actions.LiftInfo.DeleteliftResult.LiftIsNotExist:
                        errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("LiftInfoAdd") as string);
                        break;
                    case Data.BLL.Actions.LiftInfo.DeleteliftResult.OK:
                        errmsg.Text = this.TryFindResource("successfuldelete") as string;
                        this.liftinfoList.ItemsSource = Config.LiftInftList = Actions.LiftInfo.List("");
                        this.liftinfoList.Items.Refresh();
                        string msg = "=InfoDelectLift," + liftNum.Text.Trim();
                        Actions.Log.AddOperation.Invoke(msg);//string.Format((string)this.TryFindResource("InfoDelectLift"), liftNum.Text.Trim()));
                        liftNum.Text = "1";
                        liftType.SelectedIndex = 0;
                        liftGroup.SelectedIndex = 0;
                        liftLine.SelectedIndex = 0;
                        liftLineSerial.SelectedIndex = 0;
                        liftCOM.SelectedIndex = 0;
                        liftActualFloor.Text = "";
                        liftModel.Text = "";
                        liftSite.Text = "";
                        typeLibNum.SelectedIndex = 0;
                        liftShortName.Text = "";
                        liftAngle.SelectedItem = 0;
                        liftScreen.SelectedIndex = 0;
                        if (null != Actions.LiftInfo.ReLoadUI)
                            Actions.LiftInfo.ReLoadUI();
                        break;
                }
            }
        }

        Data.Model.LiftInfo GetLiftFromUI(Data.Model.LiftInfo row)
        {
            row.LiftNum = liftNum.Text.Trim().toNumeric();
            row.LiftType = (LiftType)Convert.ToInt32(liftType.SelectedValue);
            row.LiftGroup = Convert.ToInt32(liftGroup.SelectedItem);
            row.LiftLine = Convert.ToInt32(liftLine.SelectedItem);
            row.LiftLineSerial = Convert.ToInt32(liftLineSerial.SelectedItem);
            row.LiftActualFloor = liftActualFloor.Text.Trim();
            row.LiftModel = liftModel.Text.Trim();
            row.LiftSite = liftSite.Text.Trim();
            row.LiftCOM = liftCOM.SelectedValue.ToString();
            row.Screen = Convert.ToInt32(liftScreen.SelectedItem);
            row.TypeLibNum = typeLibNum.SelectedValue.ToString();
            row.ShortName = liftShortName.Text.Trim();
            row.Angle = Convert.ToInt32(liftAngle.SelectedValue);
            return row;
        }




        void add343_Click(object sender, RoutedEventArgs e)
        {
            delForMore_Click(null, null);
            int num = 0;
            string lib = "001";
            for (int i = 1; i <= 30; i++)
            {
                addLift(++num, LiftType.电梯, lib);
            }
            lib = "002";
            for (int i = 1; i <= 40; i++)
            {
                addLift(++num, LiftType.扶梯, lib);
            }
            for (int i = 1; i <= 30; i++)
            {
                addLift(++num, LiftType.人行步道, lib);
            }
            this.liftinfoList.ItemsSource = Config.LiftInftList = Actions.LiftInfo.List("");
            this.liftinfoList.Items.Refresh();
            if (null != Actions.LiftInfo.ReLoadUI)
                Actions.LiftInfo.ReLoadUI();
            canvasForMore.Visibility = Visibility.Hidden;
        }

        private void addLift(int num, LiftType liftType, string lib)
        {
            string com = "COM" + (int)Math.Ceiling(num / 25.0);
            Data.Model.LiftInfo row = new Data.Model.LiftInfo();

            row.LiftNum = num;
            row.ShortName = "L" + num;
            row.LiftGroup = (int)Math.Ceiling(num / 10.0);
            row.LiftLine = (int)Math.Ceiling(num / 15.0);
            row.LiftCOM = com;
            row.Screen = (int)Math.Ceiling(num / 25.0);
            row.LiftType = liftType;
            row.LiftLineSerial = 0;
            row.LiftActualFloor = "B3,B2,B1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20";
            row.LiftSite = num + "号梯";
            row.LiftModel = "c";
            row.TypeLibNum = lib;
            row.Angle = 0;
            row.Margin = "0.00,0.00";

            Actions.LiftInfo.AddLiftInfo(row);
        }

        void delForMore_Click(object sender, RoutedEventArgs e)
        {
            Actions.LiftInfo.DeleteAll();

            this.liftinfoList.ItemsSource = Config.LiftInftList = Actions.LiftInfo.List("");
            this.liftinfoList.Items.Refresh();
            if (null != Actions.LiftInfo.ReLoadUI)
                Actions.LiftInfo.ReLoadUI();
            //canvasForMore.Visibility = Visibility.Hidden;
        }

        void okForMore_Click(object sender, RoutedEventArgs e)
        {
            int num = 0;
            try
            {
                num = Data.DAL.LiftInfo.GetMaxID();
            }
            catch { num = 0; }
            string com = "COM1";
            if (null != liftCOM.SelectedValue)
                com = liftCOM.SelectedValue.ToString();
            string lib = "001";
            if (null != typeLibNum.SelectedValue)
                lib = typeLibNum.SelectedValue.ToString();

            //分几个COM添加
            int g = Convert.ToInt32(numOfCOM.SelectedItem);

            int c = com.Substring(3).toInt32();

            int length = 100;
            if (!string.IsNullOrEmpty(lengthForMore.Text.Trim()))
                length = lengthForMore.Text.Trim().toInt32();
            for (int i = 1; i <= g; i++)
            {
                do
                {
                    com = "COM" + (c + i - 1);
                    Data.Model.LiftInfo row = new Data.Model.LiftInfo();
                    num++;
                    row.LiftNum = num;
                    row.ShortName = "L" + num;
                    row.LiftGroup = (int)Math.Ceiling(num / 10.0);
                    row.LiftLine = (int)Math.Ceiling(num / 15.0);
                    row.LiftCOM = com;
                    row.Screen = (int)Math.Ceiling(num / 25.0);
                    row.LiftType = 0;
                    row.LiftLineSerial = 0;
                    row.LiftActualFloor = "B3,B2,B1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61";
                    row.LiftSite = num + "号梯";
                    row.LiftModel = "c";
                    row.TypeLibNum = lib;
                    row.Angle = 0;
                    row.Margin = "0.00,0.00";

                    Actions.LiftInfo.AddLiftInfo(row);

                }
                while (num != (length / g) * i);
            }

            this.liftinfoList.ItemsSource = Config.LiftInftList = Actions.LiftInfo.List("");
            this.liftinfoList.Items.Refresh();
            if (null != Actions.LiftInfo.ReLoadUI)
                Actions.LiftInfo.ReLoadUI();
            canvasForMore.Visibility = Visibility.Hidden;
        }

        void addmore_Click(object sender, RoutedEventArgs e)
        {
            liftCOMForMore.ItemsSource = liftCOM.ItemsSource;
            liftCOMForMore.SelectedIndex = 0;

            typeLibNumForMore.ItemsSource = typeLibNum.ItemsSource;
            typeLibNumForMore.DisplayMemberPath = "TypeLibNum";
            typeLibNumForMore.SelectedValuePath = "TypeLibNum";
            typeLibNumForMore.SelectedIndex = 0;
            canvasForMore.Visibility = Visibility.Visible;
            numOfCOM.ItemsSource = new int[] { 1, 2, 3, 4 };
            numOfCOM.SelectedIndex = 0;
        }

        void LiftInfo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F8)
                this.addmore.Visibility = Visibility.Visible;
        }

    }
}
